package com.backstage.service.Impl;

import com.backstage.mapper.SysRoleMapper;
import com.backstage.pojo.dto.Message;
import com.backstage.pojo.entity.SysRole;

import com.backstage.pojo.vo.SysRoleVo;
import com.backstage.service.SysRoleService;
import com.backstage.util.PojoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service("SysRoleService")
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    SysRoleMapper sysRoleMapper;

    /**
     * 查询
     * @param sysRoleVo
     * @return
     */
    @Override
    public Message<List<SysRoleVo>> roleList(SysRoleVo sysRoleVo) {
        Page<SysRole> page=new Page<>();
        page.setCurrent(sysRoleVo.getPage());
        page.setSize(sysRoleVo.getRows());
        QueryWrapper<SysRole> wrapper=new QueryWrapper<>();
        IPage<SysRole> resultPage=sysRoleMapper.selectPage(page,wrapper);
        List<SysRole> records = resultPage.getRecords();
       /* List<SysRoleVo> result=new ArrayList<>();
        for (int i = 0; i < resultPage.getRecords().size(); i++) {
            SysRole sysRole=resultPage.getRecords().get(i);
            SysRoleVo roleVo = PojoUtil.copyObject(sysRole, SysRoleVo.class);
            roleVo.setPage(sysRoleVo.getPage());
            roleVo.setRows(sysRoleVo.getRows());
            roleVo.setSysRole(sysRole);
            result.add(roleVo);
        }*/
        Message<List<SysRoleVo>> message=Message.successMessage(resultPage.getTotal(),PojoUtil.copyList(records,SysRoleVo.class));
        return message;
    }
//1072806379208708100
//1072806379208708096
//9223372036854775807

    public Message<SysRoleVo> addRole(SysRoleVo sysRoleVo) {
        //判断传入参数是否为空
        if(sysRoleVo==null)return Message.errorMessage("1002","添加角色参数未接受");

        sysRoleVo.setUpdateTime(new Date().getTime());
        sysRoleVo.setCreateTime(new Date().getTime());
        int insert = sysRoleMapper.insert(PojoUtil.copyObject(sysRoleVo,SysRole.class));
        if (insert==0){
            return Message.errorMessage("1001","角色添加失败");
        }else {
         return Message.successMessage(1L,sysRoleVo);
        }

    }

    @Override
    public Message<SysRoleVo> updateRole(SysRoleVo sysRoleVo) {


       //判断传入参数是否为空
        if(sysRoleVo==null)return Message.errorMessage("1002","角色参数未接收");
        sysRoleVo.setUpdateTime(new Date().getTime());


       //判断传入参数是否为空
        if(sysRoleVo==null)return Message.errorMessage("1002","角色参数未接收");
        sysRoleVo.setUpdateTime(new Date().getTime());



        int insert = sysRoleMapper.updateById(PojoUtil.copyObject(sysRoleVo,SysRole.class));
        if (insert==0){
            return Message.errorMessage("1001","角色修改失败");
        }else {
            return Message.successMessage(1L,sysRoleVo);
        }
    }

    @Override
    public Message<String> deleteRole(Long id) {
        int delete = sysRoleMapper.deleteById(id);
        if (delete==0){
            return Message.errorMessage("1001","角色删除失败");
        }else {
            return Message.successMessage(1L,"删除成功");
        }
    }

    @Override
    public Message<String> deleteRoleList(List<Long> roles) {
        for (int i = 0; i < roles.size(); i++) {
            Long aLong = roles.get(i);
            deleteRole(aLong);
        }
        return Message.successMessage(1L,"已经全部删除成功");
    }
}
